/**
 * 
 */
package gestionBulletins.view;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import gestionBulletins.controller.EnfantsTableModel;
import gestionBulletins.model.Adherent;
import gestionBulletins.model.Enfant;
import gestionBulletins.model.persistance.DaoAdherent;
import gestionBulletins.model.persistance.DaoEnfant;
import gestionBulletins.model.persistance.DaoFactory;
import gestionBulletins.model.persistance.DaoFactoryBuilder;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * @author paraita
 *
 */
public class InfosEnfantsWindow extends JFrame implements ActionListener, ListSelectionListener {

	private static final long serialVersionUID = 1L;
	public InfosAdherentWindow iaw;
	public JTable table;
	public EnfantsTableModel tableModel;
	private JButton boutonAjouter;
	private JButton boutonModifier;
	private JButton boutonEnlever;
	private JButton boutonFermer;
	public DaoFactory daoFactory;
	private Adherent parent;
	
	public InfosEnfantsWindow(DaoFactory daoFactory, Adherent parent, InfosAdherentWindow iaw) {
		super();
		this.daoFactory = daoFactory;
		this.parent = parent;
		this.iaw = iaw;
		setResizable(false);
		setDefaultCloseOperation(HIDE_ON_CLOSE);
		setTitle("Liste des enfants");
		initComposantsGraphiques();
		setupFrame();
		pack();
	}

	private void initComposantsGraphiques() {
		tableModel = new EnfantsTableModel(daoFactory, this, parent);
		table = new JTable(tableModel);
		boutonAjouter = new JButton("Ajouter");
		boutonModifier = new JButton("Modifier");
		boutonEnlever = new JButton("Enlever");
		boutonFermer = new JButton("Fermer la fenêtre");
		table.getSelectionModel().addListSelectionListener(this);
		boutonAjouter.addActionListener(this);
		boutonModifier.addActionListener(this);
		boutonEnlever.addActionListener(this);
		boutonFermer.addActionListener(this);
		boutonModifier.setEnabled(false);
		boutonEnlever.setEnabled(false);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	}
	
	private void setupFrame() {
		Container pane = getContentPane();
		BorderLayout bl = new BorderLayout(10,10);
		pane.setLayout(bl);
		JScrollPane sp = new JScrollPane(table);
		getRootPane().setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
		
		/* haut de page */
		JLabel sousTitre = new JLabel("Liste des enfants de " +
									  parent.getPrenom1() +
									  " " + parent.getNom());
		sousTitre.setAlignmentX(CENTER_ALIGNMENT);
		pane.add(sousTitre,
				 BorderLayout.NORTH);
		
		/* table au milieu */
		pane.add(sp, BorderLayout.CENTER);
		
		/* bas de page */
		JPanel basDePage = new JPanel();
		FlowLayout fl = new FlowLayout();
		basDePage.setLayout(fl);
		basDePage.add(boutonAjouter);
		basDePage.add(boutonModifier);
		basDePage.add(boutonEnlever);
		basDePage.add(Box.createRigidArea(new Dimension(25,0)));
		basDePage.add(boutonFermer);
		pane.add(basDePage, BorderLayout.SOUTH);
	}
	
	public void repaint() {
		//System.out.println("on me repaint !");
		tableModel.update(null, null);
		table.revalidate();
		table.repaint();
		DaoAdherent da = daoFactory.getDaoAdherent();
		parent.setNbEnfants("" + tableModel.getRowCount());
		da.update(parent);
		iaw.notifyObservers();
		super.repaint();
	}
	
	public static void main(String[] args) {
		DaoFactoryBuilder.setTypePersistance(DaoFactoryBuilder.TypePersistance.Sqlite);
		DaoFactory df = DaoFactoryBuilder.getDaoFactory();
		DaoAdherent da = df.getDaoAdherent();
		//System.out.println(da.requestById(2));
		InfosEnfantsWindow win = new InfosEnfantsWindow(df, da.requestById(2), null);
		win.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == boutonFermer) {
			setVisible(false);
		}
		if (e.getSource() == boutonAjouter) {
			AddEnfantWindow aew = new AddEnfantWindow(daoFactory, parent.getId(), true, 0);
			aew.setObserver(this);
			aew.setVisible(true);
		}
		if (e.getSource() == boutonModifier) {
			ModifEnfantWindow mew = new ModifEnfantWindow(this, parent);
			mew.setTitle("Modifications d'un enfant");
			mew.setObserver(this);
			mew.setVisible(true);
		}
		if (e.getSource() == boutonEnlever) {
			int confirmerSuppression = JOptionPane.showConfirmDialog(this,
					"Voulez vous vraiment supprimer cet enfant ?",
					"Suppression",
					JOptionPane.YES_NO_OPTION);
			if (confirmerSuppression == 0) {
				DaoEnfant de = daoFactory.getDaoEnfant();
				int numLigne = table.convertRowIndexToModel(table.getSelectedRow());
				Enfant enfant = tableModel.getEnfant(numLigne);
				de.delete(enfant);
				tableModel.removeRow(numLigne);
				repaint();
			}
		}
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		if (e.getValueIsAdjusting())
            return;
		/* si y'a pas de selection d'un enfant on desactive le bouton effacer et modifier */
		if (((ListSelectionModel)e.getSource()).isSelectionEmpty()) {
			boutonModifier.setEnabled(false);
			boutonEnlever.setEnabled(false);
		}
		/* sinon c'est bon */
		else {
			boutonModifier.setEnabled(true);
			boutonEnlever.setEnabled(true);
		}
	}
}
